<?php
   $licesneID=$_GET['licesneID'];
   $connect=mysql_connect("localhost","root","");
   mysql_select_db("news",$connect);
   $sql="select * from shouzhong where licesneid='{$licesneID}'";
   $result=mysql_query($sql);
   $row=mysql_fetch_assoc($result);
   $id=$row['id'];
   $newsid=array();
   
   if(isset($id)){
	   //查看属于哪个群
	   $sql2="select * from guanxi where sid=$id";
	   $result2=mysql_query($sql2);
	   $arr=array();
	   $array=array();
	   while($row2=mysql_fetch_array($result2)){//得到群
		   $gid=$row2['gid'];
		   //根据群找到过滤的条件
		   $sql3="select * from mygroup where id=$gid";
		   $result3=mysql_query($sql3);
		   $row3=mysql_fetch_assoc($result3);
		   $g_title=$row3['g_title'];
		   $g_author=$row3['g_author'];
		   $g_web=$row3['g_web'];
		   
		   if($g_title==null&&$g_author==null&&$g_web==null){
			  $sql4="select * from news";
			  $result4=mysql_query($sql4);
			  while($row4=mysql_fetch_array($result4)){
		           $nid=$row4['id'];
				   
				   if(isset($row4['id'])){//有符合的
				   //看是否有重复的 查受众表
					$sql6="select * from shouzhong where id='$id'";
					$result6=mysql_query($sql6);
					$row6=mysql_fetch_assoc($result6);
					$nid2=$row6['nid'];
					
					#得到真正的网站
					$shu_web=$row4['web'];
					$sql_w="select * from wangzhan where id=$shu_web";
				    $result_w=mysql_query($sql_w);
				    $row_w=mysql_fetch_assoc($result_w);
				    $g_web2=$row_w['name'];
					
				   if($nid>$nid2){//说明有新的可以发送
						$input = array("id"=>$row4['id'], "title"=>$row4['title'],"web"=>$g_web2,"author"=>$row4['author'],"publishtime"=>$row4['publishtime'],"content"=>$row4['content'],"link"=>$row4['link']);
						//array_push($array["newslist"],$input);
						//并且该变受众表中的nid
						array_push($arr,$input);
						array_push($newsid,$nid);
					}
					
				 }
				 
			}

		}else{
			 //得到标题
			 $title_array=array();
			 $title_array=explode("\n",$g_title);
			 $strt1=" (title like '%{$title_array[0]}%'";
			  for($i=1;$i<count($title_array);$i++){
			     $strt2.=" or title like '%{$title_array[$i]}%'";
			 } 
			  $rel_title=$strt1.$strt2.")";
			 
			//得到作者
			 $author_array=array();
		     $author_array=explode("\n",$g_author);
			 $stra1=" and (author like '%{$author_array[0]}%'";

		     for($i=1;$i<count($author_array);$i++){
			     $stra2.=" or author like '%{$author_array[$i]}%'";
			 } 
			 $rel_author=$stra1.$stra2.")";
			
			 
			 //得到网站
			 $web_array=array();
			 $web_array=explode(',',$g_web);
			 $strw1= $stra1=" and (web=$web_array[0]";
		     for($i=1;$i<count($web_array);$i++){
			     $strw2.=" or web=$web_array[$i]";
			 } 
			 $rel_web=$strw1.$strw2.")";
			 
			 
			 $rel_sql=$rel_title.$rel_author.$rel_web;
			 
			 
		    $sql4="select * from news where".$rel_sql;
		    $result4=mysql_query($sql4);
		  
		    $sql6="select * from shouzhong where id='$id'";
			$result6=mysql_query($sql6);
			$row6=mysql_fetch_assoc($result6);
			$nid2=$row6['nid'];
			
		   while($row4=mysql_fetch_array($result4)){
			$nid=$row4['id'];
			$web_id=$row4['web'];
			$sql_web="select * from wangzhan where id=$web_id";
			$result_web=mysql_query($sql_web);
			$row_web=mysql_fetch_assoc($result_web);
			$web2=$row_web['name'];
			
			   if(isset($row4['id'])){//有符合的
			   //看是否有重复的 查受众表
					if($nid>$nid2){//说明有新的可以发送
						$input = array("id"=>$row4['id'], "title"=>$row4['title'],"web"=>$web2,"author"=>$row4['author'],"publishtime"=>$row4['publishtime'],"content"=>$row4['content'],"link"=>$row4['link']);
						array_push($arr,$input);
						//并且该变受众表中的nid
						array_push($newsid,$nid);
					}
			    }
			}   
		}
	 }
	     $newsid =max($newsid);
	     $sql6="update shouzhong set nid=$newsid where id=$id";
		 mysql_query($sql6);
	     $array["status"]=0;
		 $arr=assoc_unique($arr,"id");
		 $array["newslist"]=$arr;
	     echo json_encode($array);  
    
	}else{
		   $array["status"]=-1;
		   echo  json_encode($array);
		
     }
	  function assoc_unique($arr, $key)
        {
		   $tmp_arr = array();
		   foreach($arr as $k => $v)
		  {
			 if(in_array($v[$key], $tmp_arr))//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
			{
			   unset($arr[$k]);
			}
		  else {
			  $tmp_arr[] = $v[$key];
			}
		  }
		sort($arr); //sort函数对数组进行排序
		return $arr;
      }
?>